<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
      <el-form-item label="搜索字段" prop="field">
        <el-input
          v-model="queryParams.field"
          placeholder="请输入字段代码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="匹配模式" prop="exp">
        <el-select v-model="queryParams.exp" placeholder="请选择匹配模式" clearable style="width:160px">
          <el-option
            v-for="dict in dev_sql_exp"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="搜索值" prop="val">
        <el-input
          v-model="queryParams.val"
          placeholder="请输入值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dataList" ref="table">
      <el-table-column type="index" width="55" align="center" />
      <el-table-column :label="f" align="center" :prop="f" v-for="(f,i) in fieldList" :key="i"/>
    </el-table>
  </div>
</template>

<script setup name="ModelData">
import { dsData } from "@/api/dev/dataset";

const { proxy } = getCurrentInstance();
const route = useRoute();
const { dev_sql_exp } = proxy.useDict('dev_sql_exp');

const dataList = ref([]);
const loading = ref(true);
const fieldList = ref([]);
const total = ref(0);
const dsCode = ref(null);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    field: null,
    exp: null,
    val: null,
  }
});

const { queryParams, form } = toRefs(data);

/** 查询模型字段列表 */
function getList() {
  loading.value = true;
  dsData(dsCode.value, queryParams.value).then(response => {
    if(response.data == null){
      dataList.value = null;
    }else if(Array.isArray(response.data)){
      let fields = [];
      for(let k in response.data[0]){
        fields.push(k);
      }
      fieldList.value = fields;
      dataList.value = response.data;
    }else{
      fieldList.value = ['total'];
      dataList.value = [{total:response.data}]
    }
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

dsCode.value = route.params && route.params.code;

getList();
</script>
